package com.apestech.api.xml.esb.modul;

import com.apestech.api.condition.ConFieldM;
import com.apestech.api.xml.esb.ConVar;
import com.apestech.api.xml.esb.EsbUtil;
import com.apestech.api.xml.esb.XmlArray;
import com.apestech.framework.dictionary.modul.Table;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.DataMappingUtil;
import com.apestech.framework.util.DateUtil;
import com.apestech.framework.util.callback.JSONMapKeyInter;
import com.apestech.framework.util.callback.SpiltStrCallback;

import java.util.ArrayList;
import java.util.Date;

public class EsbFormPub extends DataMappingUtil {

    public ArrayList<String> pubField;
    public ArrayList<String> zdPubField;
    public ArrayList<String> zdField;
    public ArrayList<String> impField;
    public ArrayList<String> stopField;
    public ArrayList<String> v_stopItemField;
    public ArrayList<String> orderZd;
    public ArrayList<String> orderCx;
    public ArrayList<String> orderCxCon;

    public JSONObject getDomain(JSONObject input) throws Exception {
        JSONObject out=new JSONObject();
        JSONArray list=new JSONArray();
        ArrayList<String> klist=input.getKeylist();
        int len=klist.size();
        for(int i=0; i<len; i++){
            String k=klist.get(i);
            JSONObject map=new JSONObject();
            map.put("field", k);
            map.put("operator","EQ");
            map.put("value", input.get(k));
            list.add(map);
        }
        //out.putAll(input);
        out.put("filters",list);
        return out;
    }

    public JSONObject getFormJson(JSONObject param, String theme) throws Exception {
        return getFormJson(param, true, theme, new JSONMapKeyInter(){
            @Override
            public Object getValue(String key, Object value) throws Exception {
                return null;
            }
        });
    }

    public JSONObject getGridJson(JSONObject param, String theme) throws Exception {
        return getFormJson(param, false, theme, new JSONMapKeyInter(){
            @Override
            public Object getValue(String key, Object value) throws Exception {
                return null;
            }
        });
    }

    private void init() throws Exception {
        if(pubField==null){
            pubField=new ArrayList<>();
            execSplitStr(ConVar.VIS_PUB, ";", new SpiltStrCallback() {
                @Override
                public void exec(String id) throws Exception {
                    pubField.add(id);
                }
            });
        }

        if(zdPubField==null) {
            zdPubField = new ArrayList<>();
            execSplitStr(ConVar.VIS_ZD_PUB, ";", new SpiltStrCallback() {
                @Override
                public void exec(String id) throws Exception {
                    zdPubField.add(id);
                }
            });
        }

        if(zdField==null) {
            zdField = new ArrayList<>();
            execSplitStr(ConVar.VIS_ZD, ";", new SpiltStrCallback() {
                @Override
                public void exec(String id) throws Exception {
                    zdField.add(id);
                }
            });
        }

        if(impField==null){
            impField=new ArrayList<>();
            execSplitStr(ConVar.VIS_IMP, ";", new SpiltStrCallback() {
                @Override
                public void exec(String id) throws Exception {
                    impField.add(id);
                }
            });
        }

        if(stopField==null){
            stopField=new ArrayList<String>();
            execSplitStr(ConVar.VIS_STOP, ";", new SpiltStrCallback() {
                @Override
                public void exec(String id) throws Exception {
                    stopField.add(id);
                }
            });
        }

        if(v_stopItemField==null){
            v_stopItemField=new ArrayList<String>();
            execSplitStr(ConVar.UNVIS_STOP_ITEM, ";", new SpiltStrCallback() {
                @Override
                public void exec(String id) throws Exception {
                    v_stopItemField.add(id);
                }
            });
        }

        if(orderZd==null){
            orderZd=new ArrayList<String>();
            execSplitStr(ConVar.ORDER_ZD, ";", new SpiltStrCallback() {
                @Override
                public void exec(String id) throws Exception {
                    orderZd.add(id);
                }
            });
        }

        if(orderCx==null){
            orderCx=new ArrayList<String>();
            execSplitStr(ConVar.ORDER_CX, ";", new SpiltStrCallback() {
                @Override
                public void exec(String id) throws Exception {
                    orderCx.add(id);
                }
            });
        }

        if(orderCxCon==null){
            orderCxCon=new ArrayList<String>();
            execSplitStr(ConVar.ORDER_CX_CON, ";", new SpiltStrCallback() {
                @Override
                public void exec(String id) throws Exception {
                    orderCxCon.add(id);
                }
            });
        }

    }

    public ArrayList<String> getOrderZd() throws Exception {
        if(orderZd==null){
            init();
        }
        return orderZd;
    }

    public ArrayList<String> getOrderCx() throws Exception {
        if(orderCx==null){
            init();
        }
        return orderCx;
    }

    public ArrayList<String> getOrderCxCon() throws Exception {
        if(orderCxCon==null){
            init();
        }
        return orderCxCon;
    }

    public boolean isInnerField(String id, String type, boolean grid) throws Exception {
        init();
        boolean flag=pubField.contains(id);
        if(!flag){
            switch (type){
                case "zd":{
                    flag=zdPubField.contains(id) || zdField.contains(id);
                    break;
                }
                case "imp":{
                    flag=zdPubField.contains(id) || impField.contains(id);
                    break;
                }
                case "stop":{
                    flag=zdPubField.contains(id) || stopField.contains(id);
                    if(grid && v_stopItemField.contains(id)){
                        flag=false;
                    }
                    break;
                }
            }
        }
        return flag;
    }

    //////////////////////////////////////
    public void setSingleField(String id, ConFieldM confieldM, String type, String remark, JSONArray con,
                                JSONObject input, boolean esb) throws Exception {
        JSONObject para=null;
        Table.Field f=null;
        if(id.equals(confieldM.getField())){
            f=confieldM.getDfield(id);
//        } else {
//            f=confieldM.getTableData().getChildData(id).getDfield();
        }
        EsbFormConditionField field=new EsbFormConditionField(type, "cx", confieldM, id, input);
        field.setComment(remark);
//        EsbFormConditionField field=new EsbFormConditionField(f.getRemarks(),id,f.getfId(),"1".equals(f.getNotNull()),
//                                                              f.getDbType(),f.getfTab()!=null,type,
//                                                              confieldM.getSession().getTheme(),input,
//                                                              confieldM.getFieldD(id),confieldM.getSysCondition());
        para=field.getField();
        if(para!=null){
            para.put("width",200);
            if(!esb){
                if(type.indexOf("grid")>=0){
                    con.add(getGridJson(para, confieldM.getSession().getTheme()));
                } else {
                    con.add(getFormJson(para, confieldM.getSession().getTheme()));
                }
            } else {
                con.add(para);
            }
        }
    }

    public JSONArray getConditionField1(JSONObject param, String theme) throws Exception {
        JSONObject modul=EsbUtil.getDivDef("condition1", theme);
        XmlArray xml=new XmlArray(null, modul, modul, param, theme);
        xml.expain();
        return xml.getOutlist();
    }

    public JSONArray getConditionField(JSONObject param, String theme) throws Exception {
        JSONObject modul=EsbUtil.getDivDef("condition", theme);
        XmlArray xml=new XmlArray(null, modul, modul, param, theme);
        xml.expain();
        return xml.getOutlist();
    }

    public boolean isMustInput(String id, String type){
        boolean flag=false;
        return flag;
    }

    /*private JSONObject addFieldJson(String f, JSONObject fieldJson) throws Exception {
        if(fieldJson==null){
            fieldJson=new JSONObject();
        }
        JSONObject field1=null;
        JSONObject field=null;
        if(formJson!=null){
            field=JSON.toJSON(formJson.get(f));
        }
        if(field!=null){
            field1= JSON.toJSON(fieldJson.get(f));
            if(field1!=null){
                field1.putAll(field);
            } else {
                field1=field;
            }
        }
        if(field1!=null){
            fieldJson.put(f, field1);
        }
        return fieldJson;
    }

    private JSONArray getFields(Table tab, boolean bform, JSONObject input, JSONObject fieldJson) throws Exception {
        if(fieldJson==null) fieldJson=new JSONObject();
        formJson=condition.getFormJSON();
        def=new JSONObject();
        JSONArray flist=new JSONArray();
        //维度类在ESB——FIELD里默认值的效果测试
        for(String k : tab.getField().getKeylist()){
            if(isInnerField(k, "zd", !bform)){
                continue;
            }
            addSingleField(k, flist, tab, input, bform);
        }
        if(fieldJson==null){
            fieldJson=new JSONObject();
        }
        if(!bform){
            fieldJson.put("detail_default", def);
        }
        return flist;
    }*/


    /*private void addSingleField(String id, JSONArray flist, Table tab, JSONObject input, boolean bform)throws Exception {
        /*JSONObject fDef=EsbUtil.getfieldD(id);
        String oper="pp_operate";
        if(!"cx".equals(type)){
            oper="pp_search";
        }
        ConFieldM confieldM=new ConFieldM(id, tab, condition.getSession(), oper, "A", formJson);
        if(fDef!=null){
            JSONObject m= (JSONObject) fieldJson.get(id);
            if(m==null){
                m=fDef;
            } else {
                m.putAll(fDef);
            }
            fieldJson.put(id, m);
        } else {
            Field cfield=condition.getControl().getField(id);
            if(cfield!=null){
                String ftype=cfield.getId();
                if(ftype!=null){
                    String ondepend=EsbUtil.getFieldChange(ftype);
                    if(ondepend!=null){
                        JSONObject m=new JSONObject();
                        m.put("depend_cdata", ondepend);
                        m.put("depend", new JSONObject());
                        fieldJson.put(id, m);
                    }
                }
            }
        }

        fieldJson=addFieldJson(id, fieldJson);
        Table.Field f=confieldM.getDfield(id);
//        EsbFormConditionField field=new EsbFormConditionField(f.getRemarks(), id, f.getfId(), "1".equals(f.getNotNull()),f.getDbType(),
//                f.getfId()!= null && (f.getfTab()==null || !f.getfTab().equals(ztab.getTabname())),
//                "bill_query", theme, input, fieldJson, confieldM.getSysCondition());
        EsbFormConditionField field=new EsbFormConditionField("bill_query",confieldM,id,input);
        field.setEsb(true);
        JSONObject param=field.getField();
        if(param!=null){
            JSONObject out=new JSONObject();
            if(bform){
                out.putAll(param);
            } else{
                String t=param.getString("type");
                if(param.get("width")==null){
                    if(t==null) param.put("width",100);
                    else {
                        switch (t){
                            case "select":{
                                param.put("width",200);
                                break;
                            }
                            default:param.put("width",100);
                        }
                    }
                }
                if(t.toLowerCase().indexOf("number")>=0){
                    if(param.get("defaultValue")==null){
                        param.put("defaultValue","0");
                        def.put(id, "0");
                    }
                }
                if("f_pub".equals(out.getString("cellEditor"))) out.remove("cellEditor");
//                    out=getGridJson(param, theme);
                out.putAll(param);
                if("f_pub".equals(out.getString("cellEditor"))) out.remove("cellEditor");
            }
            if(bform){
                String fid=id;
                int expiring=condition.getExpiring();
                if(param.get("defaultValue")==null){
                    if(fid.equals("START_DATE")){
                        out.put("defaultValue", DateUtil.dateTimeToStr(new Date()));
                    } else if(fid.equals("END_DATE")){
                        Date startDate=new Date();
                        Date endDate= DateUtil.addByDate(startDate,expiring);
                        out.put("defaultValue",DateUtil.dateTimeToStr(endDate));
                    } else if(fid.equals("CONTROL_TYPE")){
                        out.put("defaultValue",EsbUtil.getDownData(fid, "0"));  //JSON.toJSONString(
                    } else if(fid.equals("PRIORITY")){
                        out.put("defaultValue",EsbUtil.getDownData(fid, "0"));  //JSON.toJSONString(
                    } else if(fid.equals("UPDATE_VALUE")){
                        out.put("defaultValue",false);
                    }
                }
            }
            if(out.get("templateOptions.required")!=null){
                out.put("required", out.get("templateOptions.required"));
                out.remove("templateOptions.required");
            }
            if(out.get("templateOptions.disabled")!=null){
                if(out.get("disabled")==null){
                    out.put("disabled", out.get("templateOptions.disabled"));
                }
                out.remove("templateOptions.disabled");
            }
            flist.add(out);
        }
    }*/


}
